package com.lleice.web.controller.sys;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lleice.common.annotation.Permission;
import com.lleice.common.annotation.RequestLog;
import com.lleice.service.dto.ResourceTreeParentDto;
import com.lleice.service.entity.sys.SysResource;
import com.lleice.service.qo.sys.ResourceListPageReq;
import com.lleice.service.qo.sys.ResourceReq;
import com.lleice.service.service.sys.ISysResourceService;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 菜单资源相关接口
 * </p>
 *
 * @author lizihao
 * @since 2020-11-18
 */
@Api(tags = {"菜单资源相关接口"})
@RestController
@RequestMapping("/base/sysResource")
public class SysResourceController {
	@Autowired
	private ISysResourceService iSysResourceService;
	
	@ApiOperation(value = "新增或修改资源")
	@Permission
	@RequestLog
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
	public Boolean add(@Validated @RequestBody ResourceReq resourceReq) throws Exception{
		iSysResourceService.saveOrUpdateUser(resourceReq);
	 return true;
 }
	
	@ApiOperation(value = "删除资源")
	@Permission
	@RequestLog
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Boolean delete(@RequestParam("id") String id) throws Exception{
		SysResource role = new SysResource();
		role.setId(Long.valueOf(id));
		role.setIsEnable(1);
		iSysResourceService.updateById(role);
	 return true;
 }
	
	@ApiOperation(value = "资源信息")
	@Permission
	@RequestLog
    @RequestMapping(value = "/info", method = RequestMethod.POST)
	public SysResource info(@RequestParam("id") String id) throws Exception{	
	 return iSysResourceService.getById(Long.valueOf(id));
 }
	
	@ApiOperation(value = "资源列表")
	@Permission
	@RequestLog
    @RequestMapping(value = "/page", method = RequestMethod.POST)
	public IPage<SysResource> page(@RequestBody ResourceListPageReq pageReq) throws Exception{
		QueryWrapper<SysResource> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("is_enable", 0);
		queryWrapper.like(StrUtil.isNotBlank(pageReq.getResourceName()),"resource_name", pageReq.getResourceName());
		queryWrapper.like(StrUtil.isNotBlank(pageReq.getResourceCode()),"resource_code", pageReq.getResourceCode());
		queryWrapper.eq(null != pageReq.getResourceType(),"resource_type", pageReq.getResourceType());
		queryWrapper.like(StrUtil.isNotBlank(pageReq.getResourcePath()),"resource_path", pageReq.getResourcePath());
	 return iSysResourceService.page(pageReq.getPage(), queryWrapper);
 }
	
	@ApiOperation(value = "资源树结构")
	@Permission
	@RequestLog
    @RequestMapping(value = "/tree", method = RequestMethod.POST)
	public List<ResourceTreeParentDto> tree() throws Exception{
	 return iSysResourceService.getAllResource();
 }
	
	@ApiOperation(value = "资源树结构-带角色拥有的资源")
	@ApiImplicitParams({
	    @ApiImplicitParam(name="roleId",value="角色id",required=false,paramType="query")
	})
	@Permission
	@RequestLog
    @RequestMapping(value = "/treeWithUser", method = RequestMethod.POST)
	public List<ResourceTreeParentDto> treeWithUser(@RequestParam("roleId") Long roleId) throws Exception{	
	 return iSysResourceService.getAllResourceWithUser(roleId);
 }

}
